// 格式化时间
function formatTime(number, format) {
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];
  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push((date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  returnArr.push(date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
  returnArr.push(date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
  returnArr.push(date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
  returnArr.push(date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

function convertBD09ToGCJ02(lng, lat) {
  const xpi = Math.PI * 3000.0 / 180.0;
  const x = lng - 0.0065;
  const y = lat - 0.006;
  const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * xpi);
  const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * xpi);
  // const lng = z * Math.cos(theta);
  // const lat = z * Math.sin(theta);
  return {
    lng: z * Math.cos(theta),
    lat: z * Math.sin(theta),
  }
}

function convertGCJ02TOBD09(lng, lat) {
  const xpi = Math.PI * 3000.0 / 180.0;
  const x = lng, y = lat;
  const z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * xpi);
  const theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * xpi);
  return {
    lng: z * Math.cos(theta) + 0.0065,
    lat: z * Math.sin(theta) + 0.006,
  }
}


module.exports = {
  formatTime,
  convertBD09ToGCJ02,
  convertGCJ02TOBD09,
}